package modelLayer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;

/**
 * 
 * @author Nick D Pedersen 
 * @version 1.0
 * 
 */
public class Forest implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Owner owner;
    private String name;
    private String location;
    private ArrayList<Tree> trees;

    /**
     * Constructor for objects of class Forest
     * @Param String name - The name of the forest
     * @Param String location - The location of the forest
     */
    public Forest(String name, String location){
        this.name = name;
        this.location = location;
        trees = new ArrayList<Tree>();
    }
    
    /**
     * Get'n Setters:
     */
    
    
    
    public void addTree(Tree t){
        trees.add(t);
    }
    
    public Owner getOwner() {
		return owner;
	}

	public void setOwner(Owner owner) {
		this.owner = owner;
	}

	public String getName(){
        return name;
    }
    
    public void setName(String name){
        this.name = name;
    }
    
    public String getLocation(){
        return location;
    }
    
    public void setLocation(String location){
        this.location = location;
    }
    
    /**
     * getSquareKilometer - Returns the Square Kilometer
     * @Return double - The Square Kilometers of this Forest Object
     */
    public double getSquareKilometer(){
        double sk = 0;
        if(!trees.isEmpty()){
            for(Tree t : trees){
                sk += t.getSquareKilometer();
            }
        }
        return sk;
    }
    
    /**
     * findTree - Finds the Tree Object by a sort
     * @Param String sort - The sort to search for
     * @Return Tree - The Tree object
     */
    public Tree findTree(String sort){
        Tree t = null;
        for(Tree tree : trees){
            if(tree.getSort().toLowerCase().equals(sort.toLowerCase())){
                t = tree;
            }
        }
        return t;
    }
    
    /**
     * containsTree - Checks if the Tree exists in this Forest Object
     * @Return boolean - if the Forest Object contains the Tree True / else false
     */
    public boolean containsTree(Tree tree){
        boolean re = false;
        if(trees.contains(tree)){
            re = true;
        }
        return re;
    }

    /**
     * getTrees - Returns the ArrayList, containing all the Trees owned by this Object
     * @Return ArrayList<Tree> - The Trees of this Object
     */
    public ArrayList<Tree> getTrees(){
        return trees;
    }
    
    /**
     * removeTree - Removed a tree from the Forest Object
     * @Param Tree - The Tree to be removed
     */
    public void removeTree(Tree tree){
        Iterator<Tree> it = trees.iterator();
        while(it.hasNext()){
            Tree t = it.next();
            if(t == tree){
                it.remove();
            }
        }
    }
    
    /**
     * Prints Forest class information.
     * Togerther with Tree, Owner and the profit calculation, IF they are ever created.
     */
    public String toString(){
        String s = name + " / Location: " + location + " / Km2: " + getSquareKilometer();
        return s;
    }
}
